﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;

/// <summary>
/// 创建文件下载器
/// </summary>
public class FsmCreatePackageDownloader : IStateNode
{
	private StateMachine _machine;

	void IStateNode.OnCreate(StateMachine machine)
	{
		_machine = machine;
	}
	void IStateNode.OnEnter()
	{
		PatchEventDefine.PatchStatesChange.SendEventMessage("创建补丁下载器！");
		GameManager.Instance.StartCoroutine(CreateDownloader());
	}
	void IStateNode.OnUpdate()
	{
	}
	void IStateNode.OnExit()
	{
	}

	IEnumerator CreateDownloader()
	{
		yield return new WaitForSecondsRealtime(0.5f);

		var packageName = (string)_machine.GetBlackboardValue("PackageName");
		var package = YooAssets.GetPackage(packageName);
		int downloadingMaxNum = 10;
		int failedTryAgain = 3;
		var downloader = package.CreateResourceDownloader(downloadingMaxNum, failedTryAgain);
		_machine.SetBlackboardValue("Downloader", downloader);

		if (downloader.TotalDownloadCount == 0)
		{
			Debug.Log("未找到任何下载文件 !");
			_machine.ChangeState<FsmUpdaterDone>();
		}
		else
		{
			// 发现新更新文件后，挂起流程系统
			// 注意：需要在下载前检测磁盘空间不足
			int totalDownloadCount = downloader.TotalDownloadCount;
			long totalDownloadBytes = downloader.TotalDownloadBytes;
			PatchEventDefine.FoundUpdateFiles.SendEventMessage(totalDownloadCount, totalDownloadBytes);
		}
	}
}